/**
 * Author:   e20160504
 * Date:     2018/6/7 21:21
 * Description:
 */
package com.kevin.demo.exception;


import com.kevin.demo.enums.ResultEnum;
import com.kevin.demo.framwork.Result;
import com.kevin.demo.framwork.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof MyException){
            MyException myException = (MyException) e;
            return ResultUtils.error(myException);
        }
        logger.error(ResultEnum.UNKNOW_ERROR.getMsg()+ "{} ",e);
        return ResultUtils.error(ResultEnum.UNKNOW_ERROR);
    }

}
